home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / ENTRY.SWG / 0002_How to readln longer than 255 chars.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  947 b   |  36 lines

  1. {
  2.  
  3. Q:  How can I readln() from a file when the lines are longer
  4. than 255 bytes?
  5.  
  6. A:  ReadLn will accept an array [0..something] of Char as
  7. buffer to put the read characters in and it will make a proper
  8. zero-terminated char out of them. The only limitation is this:
  9. the compiler needs to be able to figure out the size of the
  10. buffer at compile time, which makes the use of a variable
  11. declared as PChar and allocated at run-time impossible.
  12.  
  13. Workaround:
  14. }
  15.  
  16.  Type
  17.    {use longest line you may encounter here}
  18.    TLine = Array [0..1024] of Char; 
  19.  
  20.    PLine = ^TLine;
  21.  
  22.  Var
  23.    pBuf: PLine;
  24.  ...
  25.    New( pBuf );
  26.  
  27.  ...
  28.    ReadLn( F, pBuf^ );
  29.  
  30. To pass pBuf to functions that take a parameter of type Pchar, 
  31. use a typecast like PChar( pBuf ).
  32.  
  33. Note:  you can use a variable declared as of type TLine or an
  34. equivalent array of char directly, of course, but I tend to
  35. allocate anything larger than 4 bytes on the heap...
  36.